package com.ouyang.controller;


import com.ouyang.Service.UserService;
import com.ouyang.form.AddUserForm;
import com.ouyang.poji.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("userlist")
    public String getuserlist ( Model model ) {
        List<User> userList = userService.getuserlist();
        model.addAttribute("list", userList);
        return "userlist";
    }

    @RequestMapping("search")
    public String SearchUser ( String key, String value, Model model ) {
        List<User> userList = userService.search(key, value);
        model.addAttribute("list", userList);
        return "userlist";
    }

    @RequestMapping("adduser")
    public String addUser ( AddUserForm userForm ) {
        try {
            userService.adduser(userForm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/user/userlist";
    }

    @RequestMapping("deleteuser")
    public String deleteUser ( String id ) {
        try {
            userService.deleteuser(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/user/userlist";
    }

    @RequestMapping("updateuser")
    public String updateUser ( User user ) {
        try {
            userService.updateuser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/user/userlist";
    }
}
